package cn.witsky.rtp2ws.push.retry;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.witsky.rtp2ws.Server;
import cn.witsky.rtp2ws.domain.CallInfo;
import cn.witsky.rtp2ws.util.FFMPEG_Util;
import cn.witsky.rtp2ws.util.FileUtil;
import cn.witsky.rtp2ws.util.HttpDownLoad;
import io.netty.util.internal.StringUtil;

public class DownLoadTask implements Runnable {
	private static Logger logger = LoggerFactory.getLogger(DownLoadTask.class);
	CallInfo call;

	public DownLoadTask(CallInfo call) {
		this.call=call;
	}
	public String getTaskId() {
		return call.getCallId();
	}
	@Override
	public void run() {
		boolean execSuc =false;
		try {
			boolean hasPcm=call.hasPcm();
			if(hasPcm) {
				boolean isSuc=false;
				if(Server.isCompress()) {
					isSuc=FileUtil.unTar(Server.backUpRtpFileDir, call.getRtp(true).getPcmTarName(), Server.downLoadDir);
					if(isSuc) {
						isSuc=FileUtil.unTar(Server.backUpRtpFileDir, call.getRtp(false).getPcmTarName(), Server.downLoadDir);
						if(!isSuc) {
							File file=new File(Server.downLoadDir+"/"+call.getRtp(true).getPcmTarName());
							file.delete();
						}
					}
				}else {
					isSuc=FileUtil.copy(Server.backUpRtpFileDir, call.getRtp(true).getPcmName(), Server.downLoadDir);
					if(isSuc) {
						isSuc=FileUtil.copy(Server.backUpRtpFileDir, call.getRtp(false).getPcmName(), Server.downLoadDir);
						if(!isSuc) {
							File file=new File(Server.downLoadDir+"/"+call.getRtp(true).getPcmName());
							file.delete();
						}
					}
				}
				if(isSuc) {
					RetryManager.execReadTask(new ReadTask(call.getCallId(), true));
					RetryManager.getRtpCount(call.getCallId()).incrementAndGet();
					RetryManager.execReadTask(new ReadTask(call.getCallId(), false));
					RetryManager.getRtpCount(call.getCallId()).incrementAndGet();
				}
			}else {
				if (!call.getRecordUrl().equals("")) {		
					String fileName = HttpDownLoad.download(call.getCallId(), call.getRecordUrl());
					logger.info("开始下载文件  call:{} 录音地址:{} fileName:{}",call.getCallId(),call.getRecordUrl(),fileName);
					if (!StringUtil.isNullOrEmpty(fileName)) {
						String suffix=FileUtil.getFileExtension(fileName);
						execSuc=FFMPEG_Util.exec(call.getCallId(), suffix);
						if (execSuc) {
							RetryManager.execReadTask(new ReadTask(call.getCallId(), true));
							RetryManager.getRtpCount(call.getCallId()).incrementAndGet();
							RetryManager.execReadTask(new ReadTask(call.getCallId(), false));
							RetryManager.getRtpCount(call.getCallId()).incrementAndGet();
						}
						clearDownLoadFile(suffix);
					}else {
						logger.warn("call:{}通过地址:{}下载文件失败",call.getCallId(),call.getRecordUrl());
					}
				}else {
					logger.warn("call:{}的录音地址是空的",call.getCallId());
				}
			}		
		}catch(Exception ex) {
			logger.error("callId"+call.getCallId()+"执行DownLoadTask出现异常:",ex);
		}finally {
			if(!execSuc) {
				RetryManager.freeRtp(call.getCallId());
			}
		}		
	}
	private  void clearDownLoadFile(String suffix) {
		logger.info("清理call:{}下载的mp3文件",call.getCallId());
		File file = new File(Server.downLoadDir + "/" + call.getCallId()+"."+suffix);
		file.delete();
		file = new File(Server.downLoadDir + "/" + call.getCallId()+"-caller."+suffix);
		file.delete();
		file = new File(Server.downLoadDir + "/" + call.getCallId()+"-called."+suffix);
		file.delete();
//		file = new File(Server.downLoadDir + "/caller-" + call.getCallId()+".pcm");
//		file.delete();
//		file = new File(Server.downLoadDir + "/called-" + call.getCallId()+".pcm");
//		file.delete();
	}
}
